/**
 * 
 */
package com.softwareinlife.xmpp.server.model;

import java.io.Serializable;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.xmpp.JID;
import com.softwareinlife.xmpp.server.BaseConstants;
import com.softwareinlife.xmpp.server.util.JIDUtil;

/**
 * @author jangsunjin
 * 
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class JIDModel implements Serializable {

	/**
	 * Serial Version UID
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Full JID
	 */
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private String JID = null;

	/**
	 * User ID
	 */
	@Persistent
	private String node = null;

	/**
	 * Domain Name
	 */
	@Persistent
	private String domain = null;

	/**
	 * Resource
	 */
	@Persistent
	private String resource = null;

	/**
	 * Constructor
	 */
	public JIDModel(String JID) {
		super();

		if (JIDUtil.isJID(JID)) {
			setJID(JID);
		}
	}

	/**
	 * Constructor
	 */
	public JIDModel(String node, String resource) {
		super();

		this.node = node;
		this.domain = BaseConstants.XMPP_DOMAIN;
		this.resource = resource;
		
		this.JID = getJID();
	}
	
	/**
	 * Constructor
	 */
	public JIDModel(String node, String domain, String resource) {
		super();
		
		this.node = node;
		this.domain = domain;
		this.resource = resource;
		
		this.JID = getJID();
	}

	/**
	 * @return the node
	 */
	public String getNode() {
		return node;
	}

	/**
	 * @param node
	 *            the node to set
	 */
	public void setNode(String node) {
		this.node = node;
	}

	/**
	 * @return the domain
	 */
	public String getDomain() {
		return domain;
	}

	/**
	 * @param domain
	 *            the domain to set
	 */
	public void setDomain(String domain) {
		this.domain = domain;
	}

	/**
	 * @return the resource
	 */
	public String getResource() {
		return resource;
	}

	/**
	 * @param resource
	 *            the resource to set
	 */
	public void setResource(String resource) {
		this.resource = resource;
	}

	/**
	 * 
	 * @return Full JID
	 */
	public String getJID() {
		if (node != null && domain != null) {
			if (resource != null) {
				return node + "@" + domain + "/" + resource;
			} else {
				return node + "@" + domain;
			}
		}

		return null;
	}
	
	/**
	 * JID Model
	 * 
	 * @return JID
	 */
	public JID getJIDModelWithoutResource() {
		return new JID(node + "@" + domain);
	}

	/**
	 * JID Model
	 * 
	 * @return JID
	 */
	public JID getJIDModel() {
		return new JID(getJID());
	}

	/**
	 * 
	 * @param JID
	 */
	public void setJID(String JID) {
		JIDModel temp = JIDUtil.parseJID(JID);

		this.node = temp.getNode();
		this.domain = temp.getDomain();
		this.resource = temp.getResource();
		
		
		this.JID = JID;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "JIDModel [node=" + node + ", domain=" + domain + ", resource="
				+ resource + "]";
	}
}
